/* js/register.js -- 参考“注册页面提交”逻辑 */
document.addEventListener('DOMContentLoaded', function() {
    const registerForm = document.querySelector('form');
    const errorSpan = document.querySelector('.error'); // 用于显示错误
  
    if (registerForm) {
      registerForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.querySelector('.username').value.trim();
        const password = document.querySelector('.password').value.trim();
        const rpassword = document.querySelector('.rpassword').value.trim();
        const nickname = document.querySelector('.nickname').value.trim();
  
        axios.post('http://localhost:8888/users/register', {
          username,
          password,
          rpassword,
          nickname
        })
        .then(response => {
          if (response.data.code === 1) {
            alert(response.data.message); // 注册成功
            window.location.href = './login.html';
          } else {
            errorSpan.textContent = response.data.message;
            errorSpan.style.display = 'block';
          }
        })
        .catch(err => {
          console.error('出错了!', err);
          errorSpan.textContent = '注册失败，请稍后再试。';
          errorSpan.style.display = 'block';
        });
      });
    }
  });
  